//
//  SettingController.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/16.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "SettingController.h"
#import "SettingView.h"
#import "LoginController.h"
#import "SettingDataCenter.h"

@interface SettingController ()<SettingViewDelegate, UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) UIImagePickerController *imagepickController;

@property (nonatomic, strong) SettingView *settingView;

@property (nonatomic, strong) LoginController *loginController;

@property (nonatomic, strong) UserModel *user;

@property (nonatomic, strong) SettingDataCenter *dataCenter;

@end

@implementation SettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.baseTitle = @"账号设置";
    self.rightBtnTitle = @"返回";
    
    
    _dataCenter = [SettingDataCenter new];
    
    [self makeSettingView];
    
    [self initImagePicker];
    
    [self loadUserInfo];
}

- (void)loadUserInfo
{
    _user = [UserManager fetchUserInfo];
    [_settingView refreshUserData:_user];
}

#pragma mark --- 实例化相机
-(void)initImagePicker
{
    //绑定调用相册的协议方法
    if (self.imagepickController == nil) {
        self.imagepickController = [[UIImagePickerController alloc]init];
        [self.imagepickController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
        
        [self.imagepickController.navigationBar setTitleTextAttributes:
         @{NSFontAttributeName:[UIFont systemFontOfSize:17.0f],
           NSForegroundColorAttributeName:[UIColor blackColor]}];
        [self.imagepickController.navigationBar setTintColor:[UIColor blackColor]];
        self.imagepickController.delegate = self;
    }
}

- (void)makeSettingView
{
    _settingView = [SettingView new];
    _settingView.delegate = self;
    [self.baseView addSubview:_settingView];
    [_settingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
}

- (void)clickAavtarUpload
{
    [self avatarUploadAction];
}

- (void)avatarUploadAction
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择一张图片作为头像" message:nil preferredStyle: UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        if (self.imagepickController != nil) {
            self.imagepickController.sourceType = UIImagePickerControllerSourceTypeCamera;
            self.imagepickController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
            self.imagepickController.allowsEditing = YES;
            [self presentViewController:self.imagepickController animated:YES completion:nil];
        }
    }];
    UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        if (self.imagepickController != nil) {
            self.imagepickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            self.imagepickController.allowsEditing = YES;
            [self presentViewController:self.imagepickController animated:YES completion:nil];
        }
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:deleteAction];
    [alertController addAction:archiveAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark --- imagePickerViewDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage * image = nil;
    if(picker.allowsEditing == NO){
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
    }else{
        image = [info objectForKey:UIImagePickerControllerEditedImage];
    }
    if(image != nil){
        
        [YJProgressHud showLoadingHud:@"正在上传..."];
        UIImage *avatar = [UIImage avatarImageWithOriginalImage:image];
        [_dataCenter uploadAvatar:avatar complation:^(id data, NSString *error) {
            if (error == nil) {
                _user.avatar = (NSString *)data;
                [UserManager saveUser:_user];
                [_settingView refreshUserData:_user];
                [YJProgressHud showSuccessHud:@"上传成功"];
            }else{
                [YJProgressHud showErrorHud:error];
            }
        }];
        
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)clickLoginOut
{
    
    [self loginOutAlert];
}

- (void)loginOutAlert
{
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"您真的要退出么？" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        [UserManager removeUser];
        [self dismissViewControllerAnimated:YES completion:nil];
        
        if ([UserManager isLogin] == NO) {
            AppDelegate *appDelegate = [AppDelegate shareDelegate];
            _loginController = [[LoginController alloc]init];
            appDelegate.window.rootViewController = _loginController;
        }
        
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    [self presentViewController:alertController animated:YES completion:nil];
}


#pragma mark ```修改昵称提交```
- (void)clickUsernameUpload:(NSString *)username
{
    [YJProgressHud showLoadingHud:@"正在修改..."];
    [_dataCenter uploadUsername:username complation:^(NSString *data, NSString *error) {
        if (error == nil) {
            _user.username = (NSString *)data;
            [UserManager saveUser:_user];
            [_settingView refreshUserData:_user];
            [YJProgressHud showSuccessHud:@"修改成功"];
        }else{
            [YJProgressHud showErrorHud:error];
        }
        
    }];
}

#pragma mark ```修改地区提交```
- (void)clickUserAreaUpload:(NSString *)userarea
{
    [YJProgressHud showLoadingHud:@"正在修改..."];
    [_dataCenter uploadAddress:userarea complation:^(NSString *data, NSString *error) {
        if (error == nil) {
            _user.address = (NSString *)data;
            [UserManager saveUser:_user];
            [_settingView refreshUserData:_user];
            [YJProgressHud showSuccessHud:@"修改成功"];
        }else{
            [YJProgressHud showErrorHud:error];
        }
        
    }];
}

#pragma mark ```修改签名提交```
- (void)clickUserSignUpload:(NSString *)usersign
{
    [YJProgressHud showLoadingHud:@"正在修改..."];
    [_dataCenter uploadSign:usersign complation:^(NSString *data, NSString *error) {
        if (error == nil) {
            _user.sign = (NSString *)data;
            [UserManager saveUser:_user];
            [_settingView refreshUserData:_user];
            [YJProgressHud showSuccessHud:@"修改成功"];
        }else{
            [YJProgressHud showErrorHud:error];
        }
        
    }];
}

- (void)clickUserBoySexUpload:(NSInteger)usersex
{
    [self uploadSex:usersex];
}

- (void)clickUserGirlSexUpload:(NSInteger)usersex
{
    [self uploadSex:usersex];
}

- (void)uploadSex:(NSInteger)sex
{
    [YJProgressHud showLoadingHud:@"正在修改..."];
    [_dataCenter uploadSex:sex complation:^(NSString *data, NSString *error) {
        if (error == nil) {
            _user.sex = (NSString *)data;
            [UserManager saveUser:_user];
            [_settingView refreshUserData:_user];
            [YJProgressHud showSuccessHud:@"修改成功"];
        }else{
            [YJProgressHud showErrorHud:error];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
